package com.yuedao.sender.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yuedao.sender.model.PositionDriver;
import com.yuedao.sender.model.PositionVehicle;
import com.yuedao.sender.service.PositionDriverRepository;
import com.yuedao.sender.service.PositionVehicleRepository;
import com.yuedao.sender.service1.PositionService;
import com.yuedao.sender.util.AppLogger;
/**
 * @author xfm
 * 2018年5月16日
 */

@RestController
@RequestMapping("/position")
public class PositionController {

	@Autowired
	PositionService positions;
	
	@Autowired
	PositionVehicleRepository positionVehicleRepository;

	@Autowired
	PositionDriverRepository positionDriverRepository;
	
	protected static AppLogger logger = AppLogger.getInstance();
	
	
	 @RequestMapping("/positionDriver")
	    public String positionDriver() {
		 List<PositionDriver> pdList = new ArrayList<PositionDriver>();
			String message = null;
			try {
		    	pdList = positionDriverRepository.findBySendFlag(0);

				message=positions.sendPositionDriver(pdList);
				
				//将发送过的数据标志字段(send_flag)更新为1
				for(int i=0;i < pdList.size();i++){
					pdList.get(i).setSendFlag(1);
				}
				positionDriverRepository.save(pdList);
			} catch (Exception e) {
				logger.error(e.getMessage());
			}
			return message;
		}
	 

	    @RequestMapping("/positionVehicle")
	    public String positionVehicle() {
	    	List<PositionVehicle> pvList = new ArrayList<PositionVehicle>();
			String message = null;
			try {
		    	pvList = positionVehicleRepository.findBySendFlag(0);

				message=positions.sendPositionVehicle(pvList);
				//将发送过的数据标志字段(send_flag)更新为1
				for(int i=0;i < pvList.size();i++){
					pvList.get(i).setSendFlag(1);
				}
				positionVehicleRepository.save(pvList);
			} catch (Exception e) {
				logger.error(e.getMessage());
			}
			return message;
		}
	 
}

